package app.project.directionlensproject.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import app.project.directionlensproject.HomeActivity;
import app.project.directionlensproject.MyApplication;
import app.project.directionlensproject.R;
import app.project.directionlensproject.adapter.CategoryAdapter;
import app.project.directionlensproject.model.Category;
import app.project.directionlensproject.serverapi.ServerAPI;
import app.project.directionlensproject.util.AsyncTask;
import app.project.directionlensproject.util.CommonUtils;
import app.project.directionlensproject.util.Constants;
import app.project.directionlensproject.util.DialogUtils;
import app.project.directionlensproject.util.GetDataTask;
import app.project.directionlensproject.util.IGetDataTask;

public class FgLogin extends Fragment implements OnClickListener {
	private Button btnRegister, btnLogin;
	private EditText edtUser, edtPass;
	private TextView tvForgetPass;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fg_login, container, false);
		btnRegister = (Button) v.findViewById(R.id.btn_register);
		btnLogin = (Button) v.findViewById(R.id.btn_login);
		edtPass = (EditText) v.findViewById(R.id.edt_pass);
		edtUser = (EditText) v.findViewById(R.id.edt_user_name);
		tvForgetPass = (TextView) v.findViewById(R.id.tv_reset_pass);

		btnRegister.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
		tvForgetPass.setOnClickListener(this);
		return v;
	}

	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btn_register) {
			((HomeActivity) getActivity()).gotoRegister();
		} else if (id == R.id.btn_login) {
			new GetDataTask(getActivity(), new IGetDataTask() {
				boolean isError = false;

				@Override
				public void onPostExecute(boolean result) throws Exception {
					super.onPostExecute(result);
					if (result) {
						CommonUtils.saveStringSetting(MyApplication.getAppContext(), Constants.USER_NAME, edtUser
								.getText().toString().trim());
						HomeActivity activity = ((HomeActivity) getActivity());
						activity.tvUserName.setText(getString(R.string.hello) + " "
								+ edtUser.getText().toString().trim());
						Toast.makeText(getActivity(), R.string.login_sucess, Toast.LENGTH_LONG).show();
						activity.gotoCategory();
						activity.isLogin = true;
					} else {
						if (isError)
							DialogUtils.showInfoDialog(getActivity(), null, getString(R.string.login_error), null);
						else
							DialogUtils.showInfoDialog(getActivity(), null, getString(R.string.login_fail), null);
					}
				}

				@Override
				public boolean doInBackGround() throws Exception {
					try {
						boolean result = ServerAPI.login(getActivity(), edtUser.getText().toString().trim(), edtPass
								.getText().toString().trim());
						return result;
					} catch (Exception e) {
						e.printStackTrace();
						isError = true;
						return false;
					}
				}
			}, true).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else if (id == R.id.tv_reset_pass) {
			((HomeActivity) getActivity()).gotoResetPass1();
		}
	}

}
